<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

    <head>
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <title>账户设置</title>
        <link rel="stylesheet" href="<c:url value="/static/wx/css/style.css"/>" />
        <script type="text/javascript" src="<c:url value="/static/wx/js/lib/rem.js"/>"></script>
          
    </head>

    <body class="befeff4">
        <div id="wrap">
            <div id="wrap_info">
                <div class="header_mine_s  mt10" style="line-height: 0.84rem;">
                    <div class="common f14 c333 tl boder-bottom relative">
                        头像
                        <c:choose>
                            <c:when test="${userVo.userImg != '' && userVo.userImg != null }">
                                <img id="header_img" class="fr" src="${userVo.userImg}" />
                            </c:when>
                            <c:otherwise>
                                <img id="header_img" class="fr" src="<c:url value="/static/wx/img/people.png"/>" />
                            </c:otherwise>
                        </c:choose>
                    </div>
                </div>

                <div class="wrap_order">
                    <div class="wrap_select">
                        <div class="wrap_name common_list">
                            <div class="common boder-bottom relative">
                                <c:choose>
	                                <c:when test="${userVo.cardName != '' && userVo.cardName != null }">
	                                    <label>联系人</label><input id="name" type="text" value="${userVo.cardName}" placeholder="输入联系人称呼" />
	                                </c:when>
	                                <c:otherwise>
	                                    <label>联系人</label><input id="name" type="text" value="${userVo.contacts }" placeholder="输入联系人称呼" />
	                                </c:otherwise>
	                            </c:choose>
                            </div>
                        </div>
                        <div class="wrap_tel common_list">
                            <div class="common boder-bottom relative">
                                <label>手机号</label><input type="text" id="tel" value="${userVo.telephone }" maxlength="11"  placeholder="输入手机号" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'0')}else{this.value=this.value.replace(/\D/g,'')}" />
                            </div>
                        </div>
                        <div class="wrap_area common_list mt10" id="goAutoAddress">
                            <div class="common boder-bottom relative">
                                <label>所在地区</label>
                                <input id="area" type="text" value="${userVo.province }${userVo.city}${userVo.area}" placeholder="输入地区" disabled="disabled" />
                            </div>
                        </div>
                        <div class="wrap_location common_list">
                            <div class="common boder-bottom relative">
                                <label>详细地址</label>
                                <input id="location" type="text" placeholder="输入详细地址" value="${userVo.address }" />
                            </div>
                        </div>
                        <!--<div class="wrap_num common_list">
                        <div class="common boder-bottom relative">
                        <label>门牌号</label><input type="text" placeholder="请输入门牌号" />
                        </div>
                    </div>-->
                    </div>
                </div>

                <div class="wrap_btn mt110">
                    <a href="javascript:void(0)" class="button" id="submit_btn">确认</a>
                </div>
                <div class="msg-error">
                    <p class="f14"></p>
                </div>
            </div>
            
             <div id="container" class="none"></div>
            <div id="myPageTop">
                <table class="common mt10">
                    <tr>
                        <td class="tc">
	                        <!-- <label class="c333 f14">苏州市：</label> -->
	                        <input class="c333 f14" id="tipinput" placeholder="输入街道、写字楼、商圈或小区名称" />
                        </td>
                    </tr>
                </table>
           </div>
        </div>
        
        <input type="hidden" id=contacts value="${userVo.contacts }" />
        <input type="hidden" id=userId value="${userVo.bId }" />
        <script type="text/javascript" src="<c:url value="/static/wx/js/lib/jquery-3.3.1.min.js"/>"></script>

        <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.10&key=30ab112c81c0a478bf33c58ae1461447&&plugin=AMap.Autocomplete,AMap.PlaceSearch"></script>
        <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
       
        <script type="text/javascript">
        	
            var prevLink = document.referrer;
        
	        //地图加载
	        var map = new AMap.Map("container", {
	            resizeEnable: true
	        });
	        //输入提示
	        var autoOptions = {
	            input: "tipinput"
	        };
	        var auto = new AMap.Autocomplete(autoOptions);
	        var placeSearch = new AMap.PlaceSearch({
	            map: map
	        });  //构造地点查询类
	        AMap.event.addListener(auto, "select", select);//注册监听，当选中某条记录时会触发
	         
	        var addressThreee , detailAddress,userId;
	        function select(e) {
	            placeSearch.setCity(e.poi.adcode);
	            placeSearch.search(e.poi.name); //关键字查询查询
	            addressThreee = e.poi.district;
	            detailAddress = e.poi.address + e.poi.name;
	            $("#area").val(addressThreee);
                $("#location").val(detailAddress);
                
                $("#myPageTop").hide();
                $("#wrap_info").show()
	        }
        
            $(document).ready(function() {
            	var contacts = $("#contacts").val();
            	var userId = $("#userId").val();
           		if (contacts == "") {
           			var getRequest = GetRequest();
                    var addressThreee = getRequest.addressThreee; 
                    var detailAddress = getRequest.detailAddress;
                    $("#area").val(addressThreee);
                    $("#location").val(detailAddress);
                }
                $("#goAutoAddress").on("click",function(){
                	$("#wrap_info").hide();
                	$("#myPageTop").show();
                })

                $("#submit_btn").on("click", function() {
                    //联系人姓名
                    var name_ = $("#name").val();
                    //联系人电话
                    var tel_ = $("#tel").val();
                    //区域
                    var area_ = $("#area").val();
                    //详细地址
                    var location_ = $("#location").val();
                    var regMobile = /^1\d{10}$/;
                    var bool = true;
                    if(!name_) {
                        alert("姓名不能为空")
                        bool = false;
                    }

                    if(!tel_) {
                        alert("电话不能为空");
                        bool = false;
                    } else if(!regMobile.test(tel_)) {
                        alert("请填入有效电话号码");
                        bool = false;
                    }

                    if(!area_) {
                        alert("区域不能为空");
                        bool = false;
                    }

                    if(!location_) {
                        alert("详细地址不能为空");
                        bool = false;
                    }

                    if(bool == true) {
                    	$.ajax({
                            type: "post",
                            url: "<c:url value="/wx/toSaveAddress.do"/>",
                            data: {
                                contacts : name_,
                                telephone : tel_,
                                area : area_,
                                address : location_,
                                id : userId
                            },
                            cache: true, 
                            async:false, 
                            dataType: "json",
                            success: function(data){
                                if (data.msg == "1") {
                                   // window.location.href = "<c:url value="/wx/toMineHome.do"/>?id="+userId;
                                	window.location.href = document.referrer;
                                }else {
                                    alert("该地区暂不支持");
                                    $(self).click(f);
                                }
                            }
                        });
                    }
                })
            })
            
            function GetRequest() {
                var url = decodeURI(location.search); //获取url中"?"符后的字串
                var theRequest = new Object();
                if(url.indexOf("?") != -1) {
                    var str = url.substr(1);
                    strs = str.split("&");
                    for(var i = 0; i < strs.length; i++) {
                        theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                    }
                }
                return theRequest;
            }
        </script>
    </body>

</html>
